<?php
/*
第一期Model的工作算是结束，目前完成的有：
1.为用户建立的__find(),__set(),__get(),__add(),__delete(),__toString()等魔术函数；
2.为用户建立了constructMapToDB()函数，可反向工程通过定义的类生成数据库的表，建立起映射。一般默认初始化对象时，检查是否存在表；
3.用户在model文件夹写的类自动会继承此类，通过core.function.php调用实现；
4.数据库层直接调用数据库类Database。
未完成工作：
1.用户查询数据接口继续做。例如：findAll()，findCondition等等。
2.级联查询。（工程量大！）
*/
include_once "database.class.php";
class Model{
	protected $ArrayType=array();
	public $id="int";
	public $db;
	
	function existTableModel($model){
		static $modelName=array();
		if (!$modelName)
			$modelName = $this->db->getTableName();
		return in_array($model,$modelName);
	}
	
	function __construct(){
		$this->db=Database::getInstance();
		$array_var=get_class_vars(get_class($this));
		foreach($array_var as $name => $type) {
			if(!in_array($name,array('ArrayType','db'))){
				if(in_array($type,array('bool','float','double','real','int','string','date','year'))){
					$this->ArrayType[$name]=$type;
				}else Error;
			}
		}
		if(!$this->existTableModel(get_class($this)))
			$this->constructMapToDB();
	}
	
	
	public function __find($id){
		$this->db->select(get_class($this),"*",$id);
		if($this->db->__get("resultNumber")==0)
			return;
		foreach($this->db->result[1] as $name => $value) {
			switch($this->ArrayType[$name]){
				case 'bool':$this->$name=(bool)$value;break;
				case 'float':$this->$name=floatval($value);break;
				case 'double':$this->$name=doubleval($value);break;
				case 'real':$this->$name=(real)doubleval($value);break;
				case 'int':$this->$name=intval($value);break;
				case 'string':$this->$name=$value;break;
				case 'date':$this->$name=$value;break;
				case 'year':$this->$name=intval($value);break;
			}
		}
		return $this;
	}
	
	public function __add(){
		$array_name=array();
		$array_value=array();
		$array_var=get_class_vars(get_class($this));
		foreach($array_var as $name => $value) {
			if(!in_array($name,array('id','ArrayType','db'))){		
					array_push($array_name,$name);
					array_push($array_value,$this->$name);
			}
		}
		$this->db->insert(get_class($this),$array_name,$array_value);
		return ;
	}
	
	public function __delete($id=0){
		if($id=0)
			if($this->id!="int")
				$this->db->__delete($this->id);
		if($id!=0)
			$this->db->__delete($id);
	}
	
	public function __findAll(){
		$this->db->select(get_class($this),'id');
		foreach($this->db->result as $key=>$ob)
			$this->db->result[$key]['id']=intval($this->db->result[$key]['id']);
		return $this->db->result;
	}
	
	public function __get($name){
		return $this->$name;
	}
	
	public function __set($name,$value){
		$this->$name=$value;
		$this->db->update(get_class($this),$this->id,$name,$value);
	}
	
	public function __getNum(){
		$this->db->select(get_class($this),'count(*)');
		return $this->db->result[1]['count(*)'];
	}
	
	public function constructMapToDB(){
		$array_var=get_class_vars(get_class($this));
		$sql="CREATE TABLE `".get_class($this)."` (`id` INT NOT NULL  AUTO_INCREMENT PRIMARY KEY ";
		foreach($array_var as $name => $type) {
			if(!in_array($name,array('dbAddress','dbAccount','db','dbPwd','dbName','conn','result','resultNumber','debug','id','ArrayType'))){
				if(in_array($type,array('bool','float','double','real','int','string','date','year'))){
					switch($type){
						case 'bool':$type="BOOL";break;
						case 'float':$type="FLOAT";break;
						case 'double':$type="DOUBLE";break;
						case 'real':$type="REAL";break;
						case 'int':$type="INT";break;
						case 'string':$type="TEXT";break;
						case 'date':$type="DATE";break;
						case 'year':$type="YEAR";break;
					}
				}else return Error;
				$sql.=",`$name` $type ";
			}
		}
		$sql.=") ENGINE = MYISAM ";
		$this->db->sqlQuery($sql);
	}
	
	public function __toString() {
		$echostring="";
		$echostring.="id=".$this->id."<br />";
		$array_var=get_class_vars(get_class($this));
		foreach($array_var as $name => $value) {
			if(!in_array($name,array('ArrayType','db','id'))){		
				$echostring.=$name."=".$this->$name."<br />";
			}
		}
		return $echostring;
	}
	
}


class FKey{
	
}

?>